package generator;

import java.awt.FlowLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import util.Configuration;


public class GeneratePanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = -2526847093170345934L;
	private JTextField configFile;
	private JTextField nameFile;
	private List startFiles;
	private List goalFiles;

	public GeneratePanel(JTextField configFile, JTextField nameFile,
			List startFiles, List goalFiles) {
		this.configFile = configFile;
		this.nameFile = nameFile;
		this.startFiles = startFiles;
		this.goalFiles = goalFiles;
		this.setLayout(new FlowLayout());
		JButton generateButton = new JButton("Generate results");
		this.add(generateButton);
		generateButton.addActionListener(this);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		int arraySize = (1 + startFiles.getItems().length + goalFiles.getItems().length);
		String fileArray[] = new String[arraySize];
		int index = 0;
		fileArray[index] = nameFile.getText();
		index++;
		for(String i : startFiles.getItems()) {
			fileArray[index] = i;
			index++;
		}
		for(String i : goalFiles.getItems()) {
			fileArray[index] = i;
			index++;
		}
		
		try {
			Configuration.loadFromXML(configFile.getText());
			Main.loadAndGenerate(fileArray);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}

}
